package com.tsd.base.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.tsd.base.dao.BaseUnitConvertorMapper;
import com.tsd.base.entity.BaseUnitConvertor;
import com.tsd.base.entity.BaseUnitConvertorExt;
import com.tsd.base.service.BaseUnitConvertorService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseUnitConvertor 服务实现层
 *
 * @author david
 * @date 2024/02/19 14:01:08
 */
@Service
public class BaseUnitConvertorServiceImpl extends BaseServiceImpl implements BaseUnitConvertorService {

    @Resource
    private BaseUnitConvertorMapper baseUnitConvertorMapper;

    @Override
    public BaseUnitConvertor getBaseUnitConvertorById(Long id) throws Exception {
        return baseUnitConvertorMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseUnitConvertorExt queryBySid(String sid) throws Exception {
        return baseUnitConvertorMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseUnitConvertors(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            BaseUnitConvertor old = baseUnitConvertorMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            baseUnitConvertorMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveBaseUnitConvertor(BaseUnitConvertor record, SysUser opUser) throws Exception {
        super.checkEmpty(record.getName(), "请输入本单位");
        super.checkEmpty(record.getTarget_name(), "请输入目标单位");
        super.checkEmpty(record.getQty(), "请输入转换数量");
        if (record.getQty().compareTo(BigDecimal.ZERO) <= 0) {
            throw new HlpException("转换数量不能小于0");
        }
        if (record.getName().equals(record.getTarget_name())) {
            throw new HlpException("本单位与目标单位不能相同:" + record.getName() + "=" + record.getQty() + record.getTarget_name());
        }
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            baseUnitConvertorMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            baseUnitConvertorMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<BaseUnitConvertorExt> findBaseUnitConvertors(Map<String, Object> params, PageBean page) throws Exception {
        return baseUnitConvertorMapper.selectByPage(params, page);
    }

    @Override
    public List<BaseUnitConvertorExt> findBaseUnitConvertors(Map<String, Object> params) throws Exception {
        return baseUnitConvertorMapper.selectByParams(params);
    }

    @Override
    public void batchSaveBaseUnitConvertor(List<BaseUnitConvertorExt> list, SysUser opUser) throws Exception {
        List<String> nameList = ListUtil.map(list, t -> t.getName() + "=" + t.getTarget_name());
        if (nameList.size() != list.size()) {
            List<String> dupList = new ArrayList<>();
            for (String name : nameList) {
                String[] names = name.split("=");
                List<BaseUnitConvertorExt> filters = ListUtil.filter(list, t -> t.getName().equals(names[0]) && t.getTarget_name().equals(names[1]));
                if (filters != null && filters.size() > 1) {
                    BaseUnitConvertorExt t = filters.get(0);
                    dupList.add(t.getName() + "=" + t.getQty() + t.getTarget_name());
                }
            }
            throw new HlpException("存在重复设置:" + ArrayUtil.join(dupList.toArray(), ","));
        }
        for (BaseUnitConvertorExt t : list) {
            this.saveBaseUnitConvertor(t, opUser);
        }
    }
}
